package com.managedBean.registration;

import java.io.Serializable;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

import org.primefaces.context.RequestContext;

import com.controller.RegistrationController;
import com.dto.registration.RegistrationDTO;
import com.managedBean.cart.CheckOutManagedBean;
import com.utils.ApplicationUtils;
import com.utils.JsfUtils;

@ManagedBean(name = "registration")
@SessionScoped
public class RegistrationManagedBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7443513504806415672L;
	private RegistrationDTO registrationDTO;
	private String chkOutType;
	private String mobileNo;
	private String password;
	private String confirmPassword;
	private String name;
	private String houseNo;
	private String street;
	private String area;
	private String landmark;
	private String city;
	private String email;
	private boolean showRegistrationDetailsFlag;
	private Integer iduserTable;
	@ManagedProperty(value = "#{registrationController}")
	private RegistrationController registrationController;
	private LoginManagedBean loginManagedBean;

	public RegistrationManagedBean() {
		registrationDTO = new RegistrationDTO();
		showRegistrationDetailsFlag = false;
		loginManagedBean = (LoginManagedBean) JsfUtils
				.getBeanInstanceFromFacesContext("login");
	}

	public void placeOrder() {
		boolean registrationSuccess = true;
		if ((chkOutType.equalsIgnoreCase("New User") || (chkOutType
				.equalsIgnoreCase("Guest")))
				&& !loginManagedBean.isLoginSuccess()) {
			registrationSuccess = register();
		}
		if (registrationSuccess) {
			CheckOutManagedBean checkOutManagedBean = (CheckOutManagedBean) JsfUtils
					.getBeanInstanceFromFacesContext("checkOut");
			checkOutManagedBean.createOrder();
		}
	}

	// TODO validate mobile no before creating record
	public boolean register() {
		System.out.println("register");
		if(null== chkOutType){
			setChkOutType("New User");
		}
		boolean flag = false;
		if (!validate()) {
			try {
				registrationDTO.setArea(area);
				registrationDTO.setCity(city);
				registrationDTO.setEmail(email);
				registrationDTO.setHouseNo(houseNo);
				registrationDTO.setLandmark(landmark);
				registrationDTO.setMobileNo(mobileNo);
				registrationDTO.setName(name);
				if (chkOutType.equalsIgnoreCase("New User")) {
					registrationDTO.setPassword(ApplicationUtils
							.securePassword(password));
				}
				registrationDTO.setStreet(street);
				registrationDTO.setRegistrationType(chkOutType
						.equalsIgnoreCase("New User") ? "N" : "G");
				iduserTable = registrationController.persist(registrationDTO);
				flag = true;
				if (!chkOutType.equalsIgnoreCase("Guest")) {
				loginManagedBean.setLoginSuccess(true);
				}
				// loginManagedBean.setUserDTO(userDTO)
				setShowRegistrationDetailsFlag(true);
				RequestContext.getCurrentInstance().execute("reg.hide()");
				RequestContext.getCurrentInstance().execute("topRightMenu");
			} catch (Exception e) {
				System.out.println(e.getMessage());
				flag = false;
			}
		} else {
			addMessage("User already exist");
			flag = false;
		}
		return flag;
	}

	private boolean validate() {
		// Validate Duplicate user or check existing record
		boolean userExistFlag = false;
		if (chkOutType.equalsIgnoreCase("New User")) {
			try {
				userExistFlag = registrationController
						.validateDuplicateUser(mobileNo);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return userExistFlag;
	}

	public void addMessage(String summary) {
		FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR,
				summary, null);
		FacesContext.getCurrentInstance().addMessage(null, message);
	}

	public void proceedRegistration() {
		showRegistrationDetailsFlag = true;
	}

	public void backToLogin() {
		showRegistrationDetailsFlag = false;
	}

	/**
	 * @return the chkOutType
	 */
	public String getChkOutType() {
		return chkOutType;
	}

	/**
	 * @param chkOutType
	 *            the chkOutType to set
	 */
	public void setChkOutType(String chkOutType) {
		this.chkOutType = chkOutType;
	}

	/**
	 * @return the registrationDTO
	 */
	public RegistrationDTO getRegistrationDTO() {
		return registrationDTO;
	}

	/**
	 * @param registrationDTO
	 *            the registrationDTO to set
	 */
	public void setRegistrationDTO(RegistrationDTO registrationDTO) {
		this.registrationDTO = registrationDTO;
	}

	/**
	 * @return the mobileNo
	 */
	public String getMobileNo() {
		return mobileNo;
	}

	/**
	 * @param mobileNo
	 *            the mobileNo to set
	 */
	public void setMobileNo(String mobileNo) {
		this.mobileNo = mobileNo;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return the confirmPassword
	 */
	public String getConfirmPassword() {
		return confirmPassword;
	}

	/**
	 * @param confirmPassword
	 *            the confirmPassword to set
	 */
	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the houseNo
	 */
	public String getHouseNo() {
		return houseNo;
	}

	/**
	 * @param houseNo
	 *            the houseNo to set
	 */
	public void setHouseNo(String houseNo) {
		this.houseNo = houseNo;
	}

	/**
	 * @return the street
	 */
	public String getStreet() {
		return street;
	}

	/**
	 * @param street
	 *            the street to set
	 */
	public void setStreet(String street) {
		this.street = street;
	}

	/**
	 * @return the area
	 */
	public String getArea() {
		return area;
	}

	/**
	 * @param area
	 *            the area to set
	 */
	public void setArea(String area) {
		this.area = area;
	}

	/**
	 * @return the landmark
	 */
	public String getLandmark() {
		return landmark;
	}

	/**
	 * @param landmark
	 *            the landmark to set
	 */
	public void setLandmark(String landmark) {
		this.landmark = landmark;
	}

	/**
	 * @return the city
	 */
	public String getCity() {
		return city;
	}

	/**
	 * @param city
	 *            the city to set
	 */
	public void setCity(String city) {
		this.city = city;
	}

	/**
	 * @return the email
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * @param email
	 *            the email to set
	 */
	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the showRegistrationDetailsFlag
	 */
	public boolean isShowRegistrationDetailsFlag() {
		return showRegistrationDetailsFlag;
	}

	/**
	 * @param showRegistrationDetailsFlag
	 *            the showRegistrationDetailsFlag to set
	 */
	public void setShowRegistrationDetailsFlag(
			boolean showRegistrationDetailsFlag) {
		this.showRegistrationDetailsFlag = showRegistrationDetailsFlag;
	}

	/**
	 * @return the registrationController
	 */
	public RegistrationController getRegistrationController() {
		return registrationController;
	}

	/**
	 * @param registrationController
	 *            the registrationController to set
	 */
	public void setRegistrationController(
			RegistrationController registrationController) {
		this.registrationController = registrationController;
	}

	/**
	 * @return the iduserTable
	 */
	public Integer getIduserTable() {
		return iduserTable;
	}

	/**
	 * @param iduserTable
	 *            the iduserTable to set
	 */
	public void setIduserTable(Integer iduserTable) {
		this.iduserTable = iduserTable;
	}

}
